package com.laiat.yamba;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.laiat.yamba.activity.PrefsActivity;

public class BootReceiver extends BroadcastReceiver {

	private static final String TAG = BootReceiver.class.getSimpleName();
	
	@Override
	public void onReceive(Context context, Intent callingIntent) {

		YambaApplication yamba = (YambaApplication)context.getApplicationContext();
		
		// Check if we should do anything at boot at all
		long updateInterval = Integer.parseInt(yamba.getPrefs().getString(PrefsActivity.KEY_UPDATE_INTERVAL, 
																		  Integer.toString(yamba.getResources().getInteger(R.integer.defaultUpdateInterval))));
		if (updateInterval == YambaApplication.UPDATE_INTERVAL_NEVER) {
			Log.d(TAG, "Update interval is never");
			return;
		}
		
		// Create the pending intent
		Intent intent = new Intent(context, UpdaterService.class);
		PendingIntent pendingIntent = PendingIntent.getService(context, -1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		
		// Setup alarm service to wake up and start service periodically
		AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
		alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, 0, updateInterval, pendingIntent);
		
		Log.d(TAG, "onReceived");
	}

	
}
